package de.bastian.hinterleitner.adaptersviews;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.bastian.hinterleitner.database.DataHelper;
import de.bastian.hinterleitner.tools.Category;
import de.hijacksoft.wyc.R;

public class CategorySelectAdapter extends ArrayAdapter<Category> {

	Context context;
	ArrayList<Category> categories;

	public CategorySelectAdapter(Context c, ArrayList<Category> cats) {
		super(c, R.layout.list_categories, cats);
		this.context = c;
		this.categories = new ArrayList<Category>();
		categories = cats;
	}
	
	@Override
	public View getDropDownView(int i, View view, ViewGroup parent) {
		return getViewAt(i, view, R.layout.list_categoriesdropdown);
	}

	@Override
	public View getView(int i, View view, ViewGroup arg2) {
		return getViewAt(i, view, R.layout.list_categories);
	}

	private View getViewAt(int i, View view, int layout) {
		if (view == null) {
			LayoutInflater inflator = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflator.inflate(layout, null);
		}
		((TextView) view.findViewById(android.R.id.text1)).setText(categories
				.get(i).name);
		String icon = categories.get(i).icon;
		if (icon != null){
			Drawable drawable = DataHelper.getDataHelper(context).getCategoryDrawable(
					categories.get(i).name);
		((ImageView) view.findViewById(R.id.list_image_icon))
				.setImageDrawable(drawable);
		}else{
			((ImageView) view.findViewById(R.id.list_image_icon))
			.setImageDrawable(null);
		}
		return view;
	}

}
